//
//  CPlayback.m
//  fsnvrPlugin
//
//  Created by fos on 12/1/14.
//  Copyright (c) 2014 foscam. All rights reserved.
//

#import "CXmlParse.h"
@implementation CXmlParse
static CXmlParse *sharedInstance = nil;
+ (instancetype)sharedXmlParse:(NSString*)file
{
    if (sharedInstance == nil) {
        sharedInstance = [[CXmlParse alloc] init];
        [sharedInstance parseXml:file];
    }
    return sharedInstance;
}

+ (void)releasXmlParse
{
    if (sharedInstance) {
        sharedInstance = nil;
    }
}

- (id)init{
    if (self = [super init]) {
        _arry = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)dealloc
{
    if (_arry) {
        _arry = nil;
    }
}

- (NSString*)GetValue:(NSString*)id
{
    if (_arry[id]) return _arry[id];
    return id;
}

- (void)parseXml:(NSString*)file
{
    if (nil == file) {
        return;
    }
    NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:@"xml"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSXMLParser* rssParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    if (rssParser) {
        [rssParser setDelegate:self];
        [rssParser setShouldProcessNamespaces:NO];
        [rssParser setShouldReportNamespacePrefixes:NO];
        [rssParser setShouldResolveExternalEntities:NO];
        [rssParser parse];
    }
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSString *errorString = [NSString stringWithFormat:@"Error code %li", (long)[parseError code]];
    NSLog(@"Error parsing XML: %@", errorString);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    	NSLog(@"parser %@",elementName);
    //NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:elementName, @"id", [attributeDict objectForKey:@"text"], elementName, nil];
    if ([attributeDict objectForKey:@"text"]) {
        [_arry setObject:[attributeDict objectForKey:@"text"] forKey:elementName];
    }
    
}
@end
